package DataStrucures;
//递归案例8皇后问题
public class Queen8 {
    int max=8;
    int[]arr=new int[max];
    static int count;
    public static void main(String[] args) {
        Queen8 queen8=new Queen8();
        queen8.check(0);
        System.out.println("一共"+count+"种解法");
    }
    public  void print(){
        for (int i = 0; i < max; i++) {
            System.out.print(arr[i]+" ");
        }
        count++;
        System.out.println();
    }
    public boolean judge(int n){
        for (int i = 0; i < n; i++) {
            if(arr[i]==arr[n]||Math.abs(n-i)==Math.abs(arr[n]-arr[i])){//判断是否是对角线或者同列
                return false;

            }
        }
        return true;
    }
    public void check(int n){
        if(n==max){
            print();
            return;
        }
        for (int i = 0; i < max; i++) {
            arr[n]=i;
            if(judge(n)){
                check(n+1);
            }
        }

    }
}
